package com.edu.filter;

import com.edu.util.CookieUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用于判断用户登录的
 *
 * @作者 five-five
 * @创建时间 2021/1/13
 */
@Component
public class LogingFilter implements HandlerInterceptor {
    @Value("${cook_login_name}")
    private final String LOGIN_COOK_NAME = "";

    /**
     * 放行前
     * 我先让他不生效
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //开始判断你这个cookie有没有值
        String cookieValue = CookieUtils.getCookieValue(request, LOGIN_COOK_NAME);
        if (StringUtils.isNotBlank(cookieValue)) {
            return true;
        } else
//        response.sendRedirect("/login");
            return true;
    }

    /**
     * 放行后
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 页面渲染后
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
